﻿using AccompanyCabinet.Helper;
using AccompanyCabinet.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AccompanyCabinet.Admin
{
    public partial class DeviceBoxList : Form
    {
        public DeviceBoxList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 每页记录数(默认：10)
        /// </summary>
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"].ToString());
        public int ppsize = 0;
        /// <summary>
        /// 总记录数(默认：0)
        /// </summary>
        public int recordCount = 0;
        /// <summary>
        /// 总页数(默认：0)
        /// </summary>
        public int pageCount = 0;
        /// <summary>
        /// 当前页(默认：1)
        /// </summary>
        public int currentPage = 1;
        private void devboxdgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void DeviceBoxList_Load(object sender, EventArgs e)
        {
            Query_module();
        }
        public void Query_module()
        {
            devboxdgv.Columns.Clear();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(" 箱子id", typeof(string)));
            dt.Columns.Add(new DataColumn("箱号", typeof(string)));
            dt.Columns.Add(new DataColumn("规格", typeof(string)));
            dt.Columns.Add(new DataColumn("锁板号", typeof(string)));
            dt.Columns.Add(new DataColumn("锁位号", typeof(string)));
            dt.Columns.Add(new DataColumn("状态", typeof(string)));
            dt.Columns.Add(new DataColumn("最后修改时间", typeof(string)));
            DataRow drs = null;
            drs = dt.NewRow();
            DBoxListpage dBoxLists = new DBoxListpage();
            string url = "TerminalInfo/DBoxList";
            string identity= ConfigurationManager.AppSettings["Identity"].ToString();
            if (currentPage==1) ppsize=pageSize;
            if (dBoxLists.PageCount == 0) pageSize = ppsize;
            var res = HttpHelper.Get(url+"?dId="+identity+ "&pageIndex="+ currentPage + "&pageSize="+ pageSize + "");
            if (res.status == "OK")
            {
                dBoxLists = JsonConvert.DeserializeObject<DBoxListpage>(res.Message);
            }
            else
            {
                MessageBox.Show(res.Message);
                return;
            }
            pageCount = dBoxLists.PageCount;
            currentPage = dBoxLists.PageIndex;
            pageSize = dBoxLists.PageSize;
            if (pageCount == currentPage)
            {
                ppsize = pageSize;
                pageSize = dBoxLists.TotalRows % pageSize;
            }
            
            if (dBoxLists.PageIndex == 1)
            {
                ppsize = pageSize; 
            }
            lab_totalnum.Text = dBoxLists.TotalRows.ToString();
            lab_totalpage.Text = dBoxLists.PageCount.ToString();
            label_pagesize.Text = dBoxLists.PageSize.ToString();
            txt_currentpage.Text = dBoxLists.PageIndex.ToString() + "/" + dBoxLists.PageCount.ToString();
            for (int i = 0; i < pageSize; i++)
            {
                drs = dt.NewRow();
                drs[0] = dBoxLists.LsList[i].id;
                drs[1] = dBoxLists.LsList[i].Num;
                drs[2] = BoxType(dBoxLists.LsList[i].TypeNo);
                drs[3] = dBoxLists.LsList[i].LockId;
                drs[4] = dBoxLists.LsList[i].LockNum;
                drs[5] = StaNo(dBoxLists.LsList[i].StatusNo);
                drs[6] = dBoxLists.LsList[i].UpdateTime;
                dt.Rows.Add(drs);
            }
            devboxdgv.DataSource = dt;
            devboxdgv.AllowUserToAddRows = false;
            devboxdgv.Columns[0].Visible = false;
        }
        public string StaNo(string status)
        {
            switch(status)
            {
                case "Use":return "在用";
                case "Unuse": return "空闲";
                default:return "占用";
            }
        }
        public string BoxType(string status)
        {
            switch (status)
            {
                case "Big": return "大";
                case "Mid": return "中";
                case "Small": return "小";
                default: return "";
            }
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            if (devboxdgv.SelectedRows.Count == 0)
            {
                MessageBox.Show("没有数据可以进行修改");
                return;
            }
            else
            {
                if (devboxdgv.SelectedRows.Count > 1)
                {
                    MessageBox.Show("只能选择一行进行修改");
                    return;
                }
                else
                {
                    int rowIndex;
                    try
                    {
                        rowIndex = devboxdgv.CurrentCell.RowIndex;
                    }
                    catch
                    {
                        rowIndex = 0;
                    }
                    string id = devboxdgv.Rows[rowIndex].Cells[0].Value.ToString();
                    string num = devboxdgv.Rows[rowIndex].Cells[1].Value.ToString();
                    string StatusNo = devboxdgv.Rows[rowIndex].Cells[2].Value.ToString();
                    string lockid = devboxdgv.Rows[rowIndex].Cells[3].Value.ToString();
                    string locknum = devboxdgv.Rows[rowIndex].Cells[4].Value.ToString();
                    Admin.UpdateDeviceBox updateDeviceBox = new UpdateDeviceBox(id,num ,StatusNo,lockid,locknum);
                    updateDeviceBox.devRefresh = this;
                    updateDeviceBox.ShowDialog();
                }
            }
        }

        private void btn_return_Click(object sender, EventArgs e)
        {
            Admin.Index index = new Index();
            this.Hide();
            index.Show();
        }

        private void btn_deletebox_Click(object sender, EventArgs e)
        {
            if (devboxdgv.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择数据");
                return;
            }
            else
            {
                if (devboxdgv.SelectedRows.Count > 1)
                {
                    MessageBox.Show("只能选择一行进行删除");
                    return;
                }
                else
                {
                    int rowIndex;
                    try
                    {
                        rowIndex = devboxdgv.CurrentCell.RowIndex;  
                    }
                    catch
                    {
                        rowIndex = 0;
                    }
                    string id = devboxdgv.Rows[rowIndex].Cells[0].Value.ToString();
                    string url = "TerminalInfo/DeleteBox";
                    var res = HttpHelper.Get(url + "?biId=" + id + "");
                    if (res.status == "OK")
                    {
                        foreach (var item in HttpHelper.dBoxLists)
                        {
                            if (item.id.Equals(Convert.ToInt32(id)))
                            {
                                HttpHelper.dBoxLists.Remove(item);
                                MessageBox.Show("删除成功");
                                Close();
                                new DeviceBoxList().Show();
                                return;
                            }       
                        }                        
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                        return;
                    }
                }
            }
            
        }

        private void btn_addbox_Click(object sender, EventArgs e)
        {
            this.Hide();
            new UpdateDeviceBox().Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            pageSize = ppsize;
            Query_module();
        }

        private void btn_prepage_Click(object sender, EventArgs e)
        {
            currentPage = currentPage - 1>0? currentPage - 1:1;
            if (currentPage + 1 >= pageCount) pageSize = ppsize;
            Query_module();
        }

        private void btn_backpage_Click(object sender, EventArgs e)
        {
            currentPage = pageCount;
            if (currentPage + 1 > pageCount) pageSize = ppsize;
            Query_module();
        }

        private void btn_nextpage_Click(object sender, EventArgs e)
        {
            currentPage = currentPage + 1>pageCount?pageCount: currentPage + 1;
            if (currentPage + 1 > pageCount) pageSize = ppsize;
            Query_module();
        }

        private void txt_currentpage_TextChanged(object sender, EventArgs e)
        {

        }

        private void label_pagesize_Click(object sender, EventArgs e)
        {

        }

        private void lab_totalpage_Click(object sender, EventArgs e)
        {

        }

        private void lab_totalnum_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void 合计_Click(object sender, EventArgs e)
        {

        }
    }
}
